//
//  AttachDownloadTask.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-6-14.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "AttachDownloadTask.h"
#import "HttpUtil.h"

@implementation AttachDownloadTask

-(void)execute
{
    NSString *folderguid = [self.params objectForKey:@"folerguid"];
    NSString *savePath = [self.params objectForKey:@"savePath"];
    NSString *url = [self.params objectForKey:@"url"];
    
    NSError *error = nil;
    NSDictionary* dictFile = [[[NSFileManager defaultManager] attributesOfItemAtPath:savePath error:&error] retain];
    long nFileSize = [dictFile fileSize];
    if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]&&nFileSize>0) {
        [self sendDataRefreshUI:savePath];
        return;
    }
    
    NSData *backData = [HttpUtil getHttpData:url];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *Dir = [NSString stringWithFormat:@"%@/Documents/attaches/%@/",NSHomeDirectory(),folderguid];
    [fileManager createDirectoryAtPath:Dir withIntermediateDirectories:YES attributes:nil error:nil];
    [fileManager createFileAtPath:savePath contents:backData attributes:nil];
    [self sendDataRefreshUI:savePath];
}

@end
